package com.guigu.pd.base.controller;

import java.util.*;
import java.util.stream.Collectors;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.guigu.common.utils.PageVo;
import com.guigu.pd.base.common.CustomIdGenerator;
import com.guigu.pd.base.entity.TruckTypeEntity;
import com.guigu.pd.base.entity.TruckTypeGoodsTypeEntity;
import com.guigu.pd.base.service.TruckTypeGoodsTypeService;
import com.guigu.pd.base.service.TruckTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.guigu.pd.base.entity.GoodsTypeEntity;
import com.guigu.pd.base.service.GoodsTypeService;
import com.guigu.common.utils.PageUtils;
import com.guigu.common.utils.R;



/**
 * 货物类型表
 *
 * @author deyran
 * @email deyran@gmail.com
 * @date 2022-07-19 19:09:01
 */
@Api(tags = "货物类型管理")
@RestController
@RequestMapping("base/goodstype")
public class GoodsTypeController {
    @Autowired
    private GoodsTypeService goodsTypeService;

    @Autowired
    private TruckTypeGoodsTypeService truckTypeGoodsTypeService;

    @Autowired
    private TruckTypeService truckTypeService;

    @Autowired
    private CustomIdGenerator customIdGenerator;



    /**
     * 新增货物类型，同时需要关联车辆类型
     * @param
     * @return 货物类型信息
     */
    @ApiOperation("新增货物类型，同时需要关联车辆类型")
    @PostMapping("/saveGoodsType")
    public R saveGoodsType(@RequestBody GoodsTypeEntity  goodsType){

        //设置货物类型id
        goodsType.setId(customIdGenerator.nextId(goodsType)+"");

        //添加货物信息
        boolean save = goodsTypeService.save(goodsType);

        TruckTypeGoodsTypeEntity truckTypeGoodsTypeEntity = new TruckTypeGoodsTypeEntity();

        boolean save1 = false;

        //添加关联车辆类型数据
        for (TruckTypeEntity truckTypeEntity : goodsType.getTruckTypeEntities()) {
            truckTypeGoodsTypeEntity.setId(customIdGenerator.nextId(truckTypeGoodsTypeEntity)+"");
            truckTypeGoodsTypeEntity.setTruckTypeId(truckTypeEntity.getId());
            truckTypeGoodsTypeEntity.setGoodsTypeId(goodsType.getId());

            save1 = truckTypeGoodsTypeService.save(truckTypeGoodsTypeEntity);

        }

        if(save1){
             return R.ok("添加成功");
        }

        return R.ok("添加失败");
    }


    /**
     * 删除货物类型--逻辑删除
     *
     * @param id 货物类型id
     * @return 返回信息
     */
    @ApiOperation("删除货物类型--逻辑删除")
    @DeleteMapping("/delectgoodtypebyid/{id}")
    public R delectgoodtypebyid(@PathVariable(name = "id") String id) {

        boolean b = goodsTypeService.removeById(id);
        if(b){
            return R.ok("删除成功");
        }
        return R.ok("删除失败");

    }

    /**
     * 根据id查询货物类型
     * @param id
     * @return
     */
    @ApiOperation("根据id查询货物类型")
    @GetMapping("/findgoodstypeById/{id}")
    public GoodsTypeEntity findgoodstypeById(@PathVariable(name = "id") String id){


        GoodsTypeEntity goodsType = goodsTypeService.getById(id);


        //还需要查询当前货物类型关联的车辆类型的id查出车辆类型信息

        List<TruckTypeEntity> ttypelist = new ArrayList<TruckTypeEntity>();

        QueryWrapper<TruckTypeGoodsTypeEntity> queryWrapper = new QueryWrapper<TruckTypeGoodsTypeEntity>();

        queryWrapper.eq("goods_type_id",id);

        List<TruckTypeGoodsTypeEntity> list = truckTypeGoodsTypeService.list(queryWrapper);

        for (TruckTypeGoodsTypeEntity truckTypeGoodsTypeEntity : list) {
            String truckTypeId = truckTypeGoodsTypeEntity.getTruckTypeId();
            //根据车辆类型id查询数据

            TruckTypeEntity byId = truckTypeService.getById(truckTypeId);

            ttypelist.add(byId);
        }


        goodsType.setTruckTypeEntities(ttypelist);

        return goodsType;
    }

    /**
     * 更新货物类型信息
     * @param goodsType
     * @return
     */
    @ApiOperation("更新货物类型信息")
    @PutMapping("/updategoodstypeById")
    public R updategoodstypeById(@RequestBody GoodsTypeEntity goodsType){


        boolean save = false;

        //更新货物类型的普通属性
        boolean b = goodsTypeService.updateById(goodsType);

        //更新关联信息
        //获取到前端传过来的车辆类型数据
        List<TruckTypeEntity> truckTypeIds = goodsType.getTruckTypeEntities();


        //判断是否有数据
        //有数据就删除重加
        if(truckTypeIds != null && truckTypeIds.size() > 0){

            QueryWrapper<TruckTypeGoodsTypeEntity> queryWrapper = new QueryWrapper<TruckTypeGoodsTypeEntity>();

            queryWrapper.eq("goods_type_id",goodsType.getId());

            //先删除车辆类型和货物类型数据
            truckTypeGoodsTypeService.remove(queryWrapper);


            TruckTypeGoodsTypeEntity truckTypeGoodsType = new TruckTypeGoodsTypeEntity();
            //重新添加数据
            for (TruckTypeEntity truckTypeId : truckTypeIds) {

                truckTypeGoodsType.setId(customIdGenerator.nextId(truckTypeGoodsType)+"");
                truckTypeGoodsType.setTruckTypeId(truckTypeId.getId());
                truckTypeGoodsType.setGoodsTypeId(goodsType.getId());

             truckTypeGoodsTypeService.save(truckTypeGoodsType);

            }

            return R.ok("修改成功");
        }

         if(b){
            return R.ok("修改成功");
           }

        return R.ok("修改失败");


    }


    /**
     * 分页查询货物类型
     * @param pageNum
     * @param pagesize
     * @return
     */
    @ApiOperation("分页查询货物类型")
    @PostMapping("/querygoodstypefy/{pageNum}/{pagesize}")
    public PageVo<GoodsTypeEntity> querygoodstypefy(@PathVariable Integer pageNum,
                                         @PathVariable Integer pagesize,
                                         @RequestBody GoodsTypeEntity goodsTypeEntity){
        PageVo<GoodsTypeEntity> list = goodsTypeService.querygoodstypefy(pageNum, pagesize,goodsTypeEntity);
        return list;
    }

    /**
     * 获取商品类型列表
     */
    @ApiOperation("获取商品类型列表")
    @GetMapping("/findgoodstypeAll")
    public List<GoodsTypeEntity> findgoodstypeAll() {

        return goodsTypeService.list();
    }
    @GetMapping("/getgtbyId/{id}")
    public String getgoodstypebyId(@PathVariable("id") String id){

        GoodsTypeEntity byId = goodsTypeService.getById(id);
        return byId.getName();
    }
}
